package org.nowcoder.leetcode.hashTable;

import java.util.HashSet;
import java.util.Set;

/**
 * Title  : 202. Happy Number
 * Source : https://leetcode.cn/problems/happy-number/?envType=study-plan-v2&envId=top-interview-150
 * Author : XrazYang
 * Date   : 2023-08-01
 */
public class LeetCode_202 {
    public boolean isHappy(int n) {
        Set<Integer> seen = new HashSet<>();
        while (n != 1 && !seen.contains(n)) {
            seen.add(n);
            int sum = 0;
            while (n > 0) { //数位分离
                int temp = n % 10;
                n = n / 10;
                sum += (temp * temp);
            }
            n = sum;
        }
        return n == 1;
    }
}
